package com.zxw.hotel.controller;

import com.zxw.hotel.common.ClientLoginInterceptor;
import com.zxw.hotel.common.Tojson;
import com.zxw.hotel.dao.ReserveDao;
import com.zxw.hotel.model.GetReserve;
import com.zxw.hotel.model.Kehu;
import com.zxw.hotel.model.clientmumber;
import com.zxw.hotel.service.KehuService;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Param;
import org.jasypt.util.password.PasswordEncryptor;
import org.jasypt.util.password.StrongPasswordEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/client")
public class IndxeController {
    public static final PasswordEncryptor pe = new StrongPasswordEncryptor();





    private KehuService kehuService;
    @Autowired
    public IndxeController(KehuService kehuService) {
        this.kehuService = kehuService;
    }
    @PostMapping("/book")
    @ResponseBody
    public Map<String, Object> book( clientmumber account,
                                    HttpSession session) {
        clientmumber mumber = (clientmumber)session.getAttribute(ClientLoginInterceptor.CLIENT_USER_KEY);
        //接收到房间名 入住时间 离店时间之后  用户名
//        account.setUserId(mumber.getUserId());
//        System.out.println(account.getUserId());
//        System.out.println(account.getRoomName());
//        System.out.println(account.getBirthFrom());
//        System.out.println(account.getBirthTo());
        account.setUserId(mumber.getUserId());
        int insert = kehuService.insert(account);

        return  Map.of("success", insert > 0);
    }
    @PostMapping("/login")
    @ResponseBody
    public Map<String, Object> login(clientmumber account, HttpSession session) {
        clientmumber member = this.kehuService.findByPhone(account.getPhone());
        if (member == null) {
            return Map.of("success", false, "error", "用户不存在");
        }
//        System.out.println(account.getPassword());
//        System.out.println(member.getPassword());

        if (pe.checkPassword(account.getPassword(), member.getPassword())) {
            session.setAttribute(ClientLoginInterceptor.CLIENT_USER_KEY, member);


            return Map.of("success", true, "data", member);
        } else {
            return Map.of("success", false, "error", "密码不正确");
        }
    }
@PostMapping("/logout")
@ResponseBody
public Map<String,Object> loginout(HttpSession session){
        if(session.getAttribute(ClientLoginInterceptor.CLIENT_USER_KEY)!=null){
            session.removeAttribute(ClientLoginInterceptor.CLIENT_USER_KEY);
            return Map.of("success",true);
        }else{
            return Map.of("success",true);
        }


}


    @GetMapping("/index")
    public String index() {
        return "client/index";
    }

    @GetMapping("/list")
    public String list(Map<String, Object> map) {
            List<Kehu> list = kehuService.findAllKehu();
            map.put("kehuList", list);
        return "client/list";
    }

    @GetMapping("/hot")
    public String hot() {
        return "client/hot";
    }

    @GetMapping("/message")
    public String message(HttpSession session,Map<String, Object> map) {
        //如果session中有用户信息 就获取用户信息 否则返回空
        if(session.getAttribute(ClientLoginInterceptor.CLIENT_USER_KEY)!=null){
            clientmumber attribute = (clientmumber)session.getAttribute(ClientLoginInterceptor.CLIENT_USER_KEY);
            Integer userId = attribute.getUserId();
            String userName = attribute.getUserName();
            List<GetReserve> getreserve = kehuService.getreserve(userId);
            //循环为getreserve添加一个userName
            for (GetReserve getReserve : getreserve) {
                getReserve.setUserName(userName);
            }
            map.put("data", getreserve);
        }


        return "client/message";
    }

    @GetMapping("/my")
    public String my(Map<String, Object> map, HttpSession session) {
        if(session.getAttribute(ClientLoginInterceptor.CLIENT_USER_KEY)!=null){
            clientmumber attribute = (clientmumber)session.getAttribute(ClientLoginInterceptor.CLIENT_USER_KEY);
           map.put("data", attribute);
        }
        return "client/my";
    }

    @GetMapping("/login")
    public String login() {
        return "client/login";
    }
    @PostMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(GetReserve getReserve) {
        int delete = kehuService.delete(getReserve);
        return Map.of("success",delete>0);
    }
}
